package com.g2room.driving.grap.life;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import com.g2room.driving.util.ImageUtil;
import com.g2room.myuall.util.DbUtil;

public class HandleImages {
	public static void main(String[] args) {
		Map<Integer, String> imageMap = getImgData();
		for(int id : imageMap.keySet())
		getImgs(id, imageMap.get(id));
	}
	
	/**
	 * 获取已经抓取的了图片sql，从d_images
	 * @return
	 */
	public static Map<Integer, String> getImgData() {
		String sql = "select id,cover from life_food_do order by id";
		Map<Integer, String> imgs = new HashMap<Integer, String>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				imgs.put(rs.getInt("id"), rs.getString("cover"));
			}
			rs.close();
			statement.close();
			DbUtil.closeConn(connection);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imgs;
	}
	
	public static void getImgs(int id, String url) {
		System.out.println(id + "====" + url);
		try {
			ImageUtil.downloadImageByType(url, "fooddo/fooddo"+id+".jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	/**
//	 * 获取所有的图片地址
//	 */
//	public static void insertImg(String sql) {
//		try {
//			Connection connection = DbUtil.getComConn();
//			PreparedStatement statement = connection.prepareStatement("insert into d_images(updateSql) values(?)");
//			statement.setString(1, sql);
//			statement.execute();
//			statement.close();
//		} catch(Exception e) {
//			e.printStackTrace();
//		}
//	}
}
